package net.sql;

import java.sql.ResultSet;
import java.util.List;

import net.dotool.page.PageModel;
import net.sql.bean.SqlParam;
import net.sql.config.CacheSqlPool;
import net.sql.config.SQLFactory;
import net.sql.config.SQLConfig;
import net.sql.i.IDbRow;
import net.sql.i.ISqlService;

/**
 * @author 李岩飞
 * @email eliyanfei@126.com
 * @date 2018年7月20日上午9:23:03
 */
public class SqlMain {

	public static void main(String[] args) {
		// queryCommon();
		// queryPage();
		count();
	}

	public static void count() {
		SQLConfig.initSqlConfig("E:\\works\\SQLParser\\config");
		SQLFactory.getConfig().loadConfig("E:\\works\\SQLParser\\config");
		ISqlService service = (ISqlService) new SqlProxy().getInstance(ISqlService.class);
		SqlParam sqlParam = new SqlParam("aa_getBean", new TestBean("4242", "10"));
		sqlParam.setTableId("apn");
		sqlParam.getParams().put("manager", 1);
		System.out.println(service.count(sqlParam));
	}
	
	/**
	 * 自定义查询
	 */
	public static void query1() {
		SQLConfig.initSqlConfig("E:\\works\\SQLParser\\config");
		SQLFactory.getConfig().loadConfig("E:\\works\\SQLParser\\config");
		ISqlService service = (ISqlService) new SqlProxy().getInstance(ISqlService.class);
		SqlParam sqlParam = new SqlParam("aa_getBean", new TestBean("4242", "10"));
		sqlParam.setTableId("apn");
		sqlParam.getParams().put("manager", 1);
		service.query(sqlParam, new IDbRow() {
			public void row(ResultSet rs) throws Exception {
				System.out.println(rs.getString("id"));
			}
		});
		System.out.println("---");
	}

	/**
	 * 普通查询，返回对象为Bean，不是默认的Map
	 */
	public static void queryCommonForBean() {
		SQLConfig.initSqlConfig("E:\\works\\SQLParser\\config");
		SQLFactory.getConfig().loadConfig("E:\\works\\SQLParser\\config");
		ISqlService service = (ISqlService) new SqlProxy().getInstance(ISqlService.class);
		SqlParam sqlParam = new SqlParam("getBean", new TestBean("4242", "10"));
		sqlParam.setTableId("apn");
		sqlParam.getParams().put("manager", 1);
		List<TestBean> obj = service.query(sqlParam);
		System.out.println(obj);
	}

	/**
	 * 分页查询
	 */
	public static void queryPage() {
		SQLConfig.initSqlConfig("E:\\works\\SQLParser\\config");
		SQLFactory.getConfig().loadConfig("E:\\works\\SQLParser\\config");
		ISqlService service = (ISqlService) new SqlProxy().getInstance(ISqlService.class);
		SqlParam sqlParam = new SqlParam("getTableData", new TestBean("4242", "10"));
		sqlParam.setTableId("customer");
		sqlParam.addParams(201807221115L);
		PageModel pageModel = new PageModel();
		sqlParam.setPageModel(pageModel);
		PageModel obj = service.pageQuery(sqlParam);
		System.out.println(obj.getData());
	}

	/**
	 * 普通查询
	 */
	public static void queryCommon() {
		SQLConfig.initSqlConfig("E:\\works\\SQLParser\\config");
		SQLFactory.getConfig().loadConfig("E:\\works\\SQLParser\\config");
		ISqlService service = (ISqlService) new SqlProxy().getInstance(ISqlService.class);
		SqlParam sqlParam = new SqlParam("getTableData", new TestBean("4242", "10"));
		sqlParam.setTableId("customer");
		sqlParam.addParams(201807221115L);
		/*
		 * sqlParam.getParams().put("lac", "4242");
		 * sqlParam.getParams().put("city_id", "10");
		 */
		List<Object> obj = service.query(sqlParam);
		System.out.println(obj);
		CacheSqlPool cachePool = SQLFactory.getConfig().getCachePool();
		System.out.println(cachePool.data);
	}
}
